<template>
  <div>
    <el-button type="primary" size="small" @click="uploadFile" style="margin-bottom: 10px">
 
      <svg-icon iconClass="upload"></svg-icon>上传{{ title }}
    </el-button>
    <el-dialog
      width="40%"
      class="warn-detail-bg"
      title="上传文件"
      :visible.sync="uploadShow"
      :close-on-click-modal="false"
      :modal-append-to-body="false"
      @close="uploadShow = false"
    >
      <div class="headerIcon">
        <svg-icon iconClass="upload"></svg-icon>
      </div>
      <div
        class="fault-dialog-body"
        v-loading="loading"
        element-loading-text="上传中,请耐心等待..."
        element-loading-spinner="el-icon-loading"
        element-loading-background="rgba(0, 0, 0, 0.3)"
      >
        <p>请上传
          <template>
            <span v-for="item in limit">{{ item.lable }}</span>
          </template>
          文件</p>
        <el-upload
          class="upload-demo"
          drag
          :multiple="false"
          :limit="1"
          ref="fileUpload"
           :headers="headers"
          :action="uploadParams.uploadUrl"
          :data="uploadParams.data"
          :file-list="uploadParams.fileLists"
          :on-success="handleSuc"
          :on-error="handleError"
          :auto-upload="false"
          :on-exceed="handleExceed"
          :on-change="changeFile"
        >
          <i class="el-icon-plus"></i>
        </el-upload>
      </div>
      <template #footer>
        <div class="dialog-footer">
          <el-button class="confirm-btn" @click="closeUpload">取 消</el-button>
          <el-button type="primary" @click="uploadForm">确 定</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>
<script>
import { mount } from '@/main';

export default {
  props: {
    // 基础类型 + 默认值
    title: {
      type: String,
      default: '默认标题',
    },
    limit: {
      type: Array,
      default: [],
    },
    url: {
      type: String,
      // default: '/agent/meeting/upload_materials',
    },
  },
  data() {
    return {
      headers:{
        Authorization:''
      },
      uploadShow: false,
      loading: false,
      uploadDoneName: '',
      uploadParams: {
        uploadUrl: '',
        data: {},
        name: 'file',
        fileLists: [],
      },
    };
  },
  mounted(){
    let token = localStorage.getItem('token')
    this.headers['Authorization'] ='Token '+token;
    this.uploadParams.uploadUrl = this.url;
  },
  methods: {

    handleExceed(files, fileList) {
      this.$message.warning('超出1个文件，请先删除当前文件，再重新上传');
      return false;
    },
    changeFile(file, fileList) {
      console.log(file,file.raw.type,this.limit);

      // 判断 this.limit 中的value 属性 是否保安 file.raw.type 中的值


      const isJPG = this.limit.some((item) => {
        return item.value === file.raw.type;
      })
        if (!isJPG) {
          this.$refs.fileUpload.clearFiles();
          this.$message.error(`文件格式校验未通过!`);
        }
       
      this.uploadParams.data = {
        file_name: file.name,
      };
    },
    handleError(err, file, fileList) {
      if (err.status == 409) {
        // 上传失败处理
        this.$message({
          message: '文件已存在，请勿重复上传。',
          type: 'warning',
        });
      } else if (err.status == 504) {
        // 上传失败处理
        this.$message({
          message: '文件较大，后台处理中，稍后请刷新查看。',
          type: 'warning',
        });
      } else {
        // 上传失败处理
        this.$message({
          message: '上传失败，请重新上传文件或联系管理员' /*  */,
          type: 'error',
        });
      }
      this.beforeClose();
    },
    closeUpload() {
      this.loading = false;
      this.uploadShow = false;
      this.$refs.fileUpload.abort();
      this.$message.warning('取消上传');
    },
    handleSuc(res, file, fileList) {
      this.uploadShow = false;
      this.loading = false;
      // 上传成功处理
      if (res && res.status == 200) {
        this.uploadShow = false;
        this.$message({
          message: '上传文件成功',
          type: 'success',
        });
        this.$emit('call-parent', file.name);
        this.uploadDoneName = file.name;
      } else {
        // 上传失败处理
        this.$message({
          message: res.message,
          type: 'error',
        });
        this.beforeClose();
      }
    },
    beforeClose() {
      // debugger
      this.loading = false;
      this.uploadShow = false;
      this.$set(this.uploadParams, 'fileLists', []);
      this.$forceUpdate();
    },
    uploadForm() {
      this.$refs.fileUpload.submit();
      this.loading = true;
    },
    uploadFile() {
      this.uploadParams.data = {
        file_name: '',
      };
      this.uploadShow = true;
      this.$set(this.uploadParams, 'fileLists', []);
      this.$forceUpdate();
    },
  },
};
</script>
